Bats: Seasonal Trends

Hallie Rutten, Shelby Cline, Monae Scott

2022-06-29

Seasonal (monthly) Trends

library(readxl)
#read in daily weather data
weather <- read_xlsx("SUD Weather Station.xlsx")
## New names:
## • `` -> `...22`
#read in hourly weather data
hourly<-read_xlsx("SUD Weather Station.xlsx", sheet=2)
## Warning: Expecting date in A2766 / R2766C1: got '11 RECORDS MISSING FROM LOGGER'
## Warning: Expecting date in A3629 / R3629C1: got 'MISSING DATA'
## Warning: Expecting date in A4604 / R4604C1: got 'MISSING DATA'
## Warning: Expecting date in A5375 / R5375C1: got 'MISSING DATA'
## Warning: Expecting date in A6071 / R6071C1: got 'MISSING DATA'
#read in recorded rain data
rain<-read_xlsx("SUD Weather Station.xlsx", sheet = 3)

#simple plot for weather (average daily temp)
###bat activity month column 1-12, rain has month column
#rename column names in weather df
colnames(weather)
##  [1] "Date Values Reflect"         "Timestamp*"                 
##  [3] "Panel Temp???"               "Air temp Avg (C)"           
##  [5] "Air Temp Max (C)"            "Time Air Temp Max"          
##  [7] "Air Temp Min"                "Time Air Temp Min"          
##  [9] "Wind Speed Avg (low) (m/S)"  "Wind Speed Max (low) (m/s)" 
## [11] "Time wind speed max (low)"   "Wind Speed Min (low) (m/s)" 
## [13] "Time wind speed min (low)"   "Wind Speed Avg (high) (m/S)"
## [15] "Wind Speed Max (high) (m/s)" "Time wind speed max (high)" 
## [17] "Wind Speed Min (high) (m/s)" "Time wind speed min (high)" 
## [19] "VWC AVG (m³/m³)"             "Solar Total (MJ/m²)"        
## [21] "ET Total (mm)"               "...22"
weather <- weather %>%
  dplyr::rename("Date" = "Timestamp*",
         AvgTemp='Air temp Avg (C)',
         Highest_Temp_Time='Time Air Temp Max',
         MaxTemp='Air Temp Max (C)',
         MinTemp='Air Temp Min',
         MaxWind='Wind Speed Max (low) (m/s)',
         AvgWind='Wind Speed Avg (high) (m/S)',
         MinWind='Wind Speed Min (low) (m/s)')

#variable with date, wind cols and temp cols
#Daily_Weather<-DailyTemp %>% 

#
hourly<-hourly %>% 
  mutate(Date= date(Timestamp)) 

#create 'rainfall' variable to get the avg. rainfall per recorded day 
rainfall<-hourly %>%  
  group_by(Date) %>% 
  summarise(total_rainfall = sum(`Rain (mm)`))


#rainfall monthly for each year
hourly<-hourly %>% 
  mutate(Month= month(Timestamp)) 

#rainfall yearly
hourly<-hourly %>% 
  mutate(Year= year(Timestamp))

yearly_rainfall <- hourly %>% 
  drop_na( Year ) %>% 
  group_by(Year) %>%
  summarise(yearly_rainfall= sum(`Rain (mm)`)) 
View(yearly_rainfall)  

#ignoring 2017 and NA bc it wasn't enough data
monthly_rainfall<-hourly %>%  
  group_by(Month,Year) %>% 
  summarise(total_monthly_rainfall = sum(`Rain (mm)`)) %>% 
  filter(Year>2017)
## `summarise()` has grouped output by 'Month'. You can override using the
## `.groups` argument.
View(monthly_rainfall)

df <- bats %>% 
  mutate(Month= month(DATE)) %>% 
  mutate(Year= year(DATE)) %>% 
  group_by(Month, Year, AUTO.ID) %>% 
  tally() %>%
  ungroup()

df <- left_join(df, monthly_rainfall, by=c('Month', 'Year'))
 
###
df %>% 
  filter(Year>2017) 
## # A tibble: 576 × 5
##    Month  Year AUTO.ID     n total_monthly_rainfall
##    <dbl> <dbl> <chr>   <int>                  <dbl>
##  1     1  2019 EPTFUS      2                   75.8
##  2     1  2019 LASCIN     11                   75.8
##  3     1  2019 LASNOC      6                   75.8
##  4     1  2020 EPTFUS     20                   26.5
##  5     1  2020 LASBOR    113                   26.5
##  6     1  2020 LASCIN   4231                   26.5
##  7     1  2020 LASNOC     96                   26.5
##  8     1  2020 MYOGRI     19                   26.5
##  9     1  2020 MYOLEI      3                   26.5
## 10     1  2020 MYOLUC      9                   26.5
## # … with 566 more rows
#plot monthly rainfall 
ggplot(data = monthly_rainfall, aes(x=Month, y=total_monthly_rainfall, fill=Year))+
  geom_area()+
  labs(title='Monthly Rainfall by Year', caption = 'Sewanee Bat Study. DataLab 2022', y='Total Rainfall (mm)')+
  facet_wrap(~Year)+
   theme( legend.position = "none" ) +
  scale_x_continuous(breaks = 1:12)

library(ggplot2)
#create Year and Month variable from Date column
df <- df %>% 
  mutate( Date = ymd(paste0( Year, "-", Month, "-1") ) )
view(df)
#filter out 2017 and 2022; not enough data
df<-df%>% 
  filter(Year>2017, Year<2022)
#convert temp.from Celsius to Fahrenheit#
#Max temp
Fahrenheit<-weather %>% 
  mutate(MaxTemp_f = MaxTemp*(9/5)+32)

#Min temp
Fahrenheit<-Fahrenheit %>% 
  mutate(MinTemp_f = MinTemp*(9/5)+32)

#Average temp
Fahrenheit<-Fahrenheit %>% 
  mutate(AvgTemp_f = AvgTemp*(9/5)+32)

#merge df and fahrenheit
#first need to create a month and year column in Fahrenheit data set

Fahrenheit<-Fahrenheit %>% 
  mutate(Month= month(Date)) %>% 
  mutate(Year= year(Date))


#Get the mean of min, max, and average temp to plot better.
df.2<-Fahrenheit %>%  
  group_by(Month, Year) %>% 
  summarise(AvgTemp_f = mean(`AvgTemp_f`),
            MaxTemp_f = mean(`MaxTemp_f`),
            MinTemp_f = mean(`MinTemp_f`))
## `summarise()` has grouped output by 'Month'. You can override using the
## `.groups` argument.
drop_na(df.2)
## # A tibble: 45 × 5
## # Groups:   Month [12]
##    Month  Year AvgTemp_f MaxTemp_f MinTemp_f
##    <dbl> <dbl>     <dbl>     <dbl>     <dbl>
##  1     1  2019      39.2      47.6      31.6
##  2     1  2020      42.6      50.9      35.1
##  3     1  2021      39.3      48.7      31.9
##  4     2  2019      48.1      57.8      39.3
##  5     2  2020      41.0      50.6      32.5
##  6     2  2021      39.3      48.3      31.2
##  7     3  2018      42.7      52.9      33.4
##  8     3  2019      47.4      56.9      37.8
##  9     3  2020      54.5      63.4      46.9
## 10     3  2021      54.1      63.1      45.8
## # … with 35 more rows
df.2<-df.2%>% 
  filter(Year>2017, Year<2022)
#plot max, min, avg temp 
ggplot(data = df.2, aes(x=Month, ymin=MinTemp_f, ymax=MaxTemp_f, y=AvgTemp_f))+
  geom_ribbon(fill = "lightpink")+
  geom_line(color="red")+
  labs(title = 'Temperature ', subtitle = 'Maximum, Minimum, and Average', caption = 'Sewanee Bat Study. DataLab 2022', y='Temperature (°F)')+
  facet_wrap(~Year)+
  scale_x_continuous(breaks = 1:12)

#wind monthly for each year
windy<-weather %>% 
  mutate(Month= month(Date)) %>% 
  mutate(Year= year(Date))

#mean of wind ?intensity? 
df.3<-windy %>%  
  group_by(Month, Year) %>% 
  summarise(AvgWind = mean(`AvgWind`),
            MaxWind = mean(`MaxWind`),
            MinWind = mean(`MinWind`))
## `summarise()` has grouped output by 'Month'. You can override using the
## `.groups` argument.
drop_na(df.3)
## # A tibble: 45 × 5
## # Groups:   Month [12]
##    Month  Year AvgWind MaxWind MinWind
##    <dbl> <dbl>   <dbl>   <dbl>   <dbl>
##  1     1  2019    3.11   10.9   0.0306
##  2     1  2020    3.27   11.6   0.0773
##  3     1  2021    2.20    9.15  0     
##  4     2  2019    3.34   10.7   0     
##  5     2  2020    3.14   11.0   0     
##  6     2  2021    2.41    9.75  0     
##  7     3  2018    2.99    9.95  0     
##  8     3  2019    2.95   10.3   0     
##  9     3  2020    2.82   10.2   0     
## 10     3  2021    3.38   11.5   0     
## # … with 35 more rows
df.3<-df.3%>% 
  filter(Year>2017, Year<2022) 

#plot wind ?intensity? 
ggplot(data = df.3, aes(x=Month, ymin=MinWind, ymax=MaxWind, y=AvgWind))+
  geom_ribbon(fill = "gray")+
  geom_line(color="darkgray")+
  labs(title='Monthly Wind Intensity', subtitle = 'By year', caption= 'Sewanee Bat Study. DataLab 2022',y='Total Wind Speed (m/s)')+
  facet_wrap(~Year)+
   theme( legend.position = "none" ) +
  scale_x_continuous(breaks = 1:12)

#overall temp. trends across the years on one graph
#just the average

#mean of temp
yearly_rainfall<-yearly_rainfall%>% 
  filter(Year>2017, Year<2022)

ggplot(data=df.2, aes(x=Month, y=AvgTemp_f, group=Year, color=Year))+
  geom_line()+
  labs(title = 'Overall Averages for Temperature ', caption = 'Sewanee Bat Study. DataLab 2022', y='Temperature (°F)')+
  scale_x_continuous(breaks = 1:12)

#plot yearly rainfall

ggplot(data = yearly_rainfall, aes(x=Year, y=yearly_rainfall, fill=Year))+
  geom_col()+
  theme( legend.position = "none" ) +
  labs(title='Overall Rainfall', caption = 'Sewanee Bat Study. DataLab 2022', y='Total Rainfall (mm)')

source('dataRead.R')
## 
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
## 
##     discard
## The following object is masked from 'package:readr':
## 
##     col_factor
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(readxl)
batspecies <- read_excel("BatSpecies.xlsx")

bats <- bats %>%  
  rename( siteID = sensor) %>% 
  select(-LATITUDE,-LONGITUDE)
# sensor data read -----
source('dataRead-sensorDates.R')
  
# join bats and sensor site data -----
bats <- left_join(bats,sensorDates, by=c('siteID','DATE'))
No.ID = c("Noise", "NoId", "NoID")  


bats<-left_join(bats,batspecies, by="AUTO.ID")
###
bats.time <- bats %>%
  filter(! AUTO.ID %in% No.ID, year < 2022) %>%
  group_by(year) %>%
  mutate(nsensors = length(unique(siteID)))
# another code segment to utilize another group_by, this time so i can see the relative frequency of the species per year
bats.time <- bats.time %>% 
  group_by(monthN, year, AUTO.ID) %>% 
  summarize(count = n(),
            rel_freq = count/nsensors)
## `summarise()` has grouped output by 'monthN', 'year', 'AUTO.ID'. You can
## override using the `.groups` argument.
bats.time <- unique(bats.time)


#this is just the plot, its copied almost exactly below so we could put it inside of an object to be used in the ggplotly function.

bats.time<-bats.time%>% 
  filter(year>2017, year<2022)

ggplot(data=bats.time,
       aes(x=monthN,
           y=rel_freq,
           color=AUTO.ID) )+
  geom_line()+
  facet_wrap(~year)+
  labs(title = 'Number of Total Calls Per Species',
       subtitle = "From 2017-2021",
       x = 'Year', y = 'Number of Calls')+
    scale_x_continuous(breaks = 1:12)

longterm_graph <- ggplot(data=bats.time, 
                         aes(x=monthN,
                             y=rel_freq, 
                             color=AUTO.ID) )+
  geom_line()+
  labs(title = 'Number of Total Calls Per Species',
       subtitle = "From 2017-2021",
       x = 'Year', y = 'Number of Calls')
# using hovertemplate allows us to zoom into the graph
ggplotly(longterm_graph, hovertemplate = paste())